###################
### ENVIRONMENT ###
###################
import os
import sys

### LOAD GSLAB MAKE
ROOT = '..'
gslm_path = os.path.join(ROOT, 'lib', 'gslab_make')
sys.path.append(gslm_path)
import gslab_make as gs

### PULL PATHS FROM CONFIG
PATHS = {
    'root': ROOT,
    'config': os.path.join(ROOT, 'config.yaml')
}
PATHS = gs.update_internal_paths(PATHS)

### LOAD CONFIG USER 
PATHS = gs.update_external_paths(PATHS)
gs.update_executables(PATHS)

############
### MAKE ###
############

## START MAKE
gs.remove_dir(['input', 'external'])
gs.clear_dir(['log'])
gs.clear_dir(['output', 'log'])
gs.start_makelog(PATHS)

for dir in ['product']:
    for subdir in ['data_generated', 'results', 'exports', 'scalars']:
        os.makedirs("output/{}/{}".format(dir, subdir), exist_ok = True)

### MAKE LINKS TO INPUT AND EXTERNAL FILES
inputs = gs.link_inputs(PATHS, ['input.txt'])
externals = gs.link_externals(PATHS, ['external.txt'])
gs.write_source_logs(PATHS, inputs + externals)
#gs.get_modified_sources(PATHS, inputs + externals)

### RUN SCRIPTS
gs.run_python(PATHS, program = 'code/submit_jobs.py')

### RUN MISSING JOBS (AFTER A FULL RUN)
gs.run_python(PATHS, program = 'code/run_missing_jobs.py')

### LOG OUTPUTS
gs.log_files_in_output(PATHS)

### CHECK FILE SIZES
#gs.check_module_size(PATHS)

### END MAKE
gs.end_makelog(PATHS)
